package com.lmaos.allcontrol.beancontrol.utils_;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtils {
	
	private Date date = new Date(System.currentTimeMillis());
	
	public static long oneWeekTime = 604800000;
	public static long oneDateTime = 86400000;
	public static long oneHourTime = 3600000;
	public static long oneMinuteTime = 60000;
	public static long oneSecondTime = 1000;
	
	public static int[] leapYearDays = {31,29,31,30,31,30,31,31,30,31,30,31}; // 闰年
	public static int[] nonleapYearDays = {31,28,31,30,31,30,31,31,30,31,30,31}; // 平年
//	public static int zoneHour = 8;
	public static TimeZone zone = TimeZone.getDefault();
	public static long zoneTime = zone.getRawOffset(); // 时区，中国时区 8小时
	
	@SuppressWarnings("deprecation")
	public int getYear(){
		date.setTime(System.currentTimeMillis());
		return date.getYear()+1900;
	}
	@SuppressWarnings("deprecation")
	public int getMonth(){
		date.setTime(System.currentTimeMillis());
		return date.getMonth()+1;
	}
	@SuppressWarnings("deprecation")
	public int getDay(){
		date.setTime(System.currentTimeMillis());
		return date.getDate();
	}
	
	@SuppressWarnings("deprecation")
	public static int getYear(Date date){
		return date.getYear()+1900;
	}
	@SuppressWarnings("deprecation")
	public static int getMonth(Date date){
		return date.getMonth()+1;
	}
	@SuppressWarnings("deprecation")
	public static int getDay(Date date){
		return date.getDate();
	}
	
	
	public static boolean sleep(long time){
		try {
			if(time <0){
				time = 0;
			}
			Thread.sleep(time);
			return true;
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		}
	}
	// After xx seconds to run
	public static boolean sleepSeconds(long seconds){
		return sleep(1000*seconds);
	}
	// After xx Minutes to run
	public static boolean sleepMinutes(long minutes)
	{
		return sleepSeconds(minutes*60);
	}
	// After xx Hours to run
	public static boolean sleepHours(long hours)
	{
		return sleepMinutes(hours*60);
	}
	// After xx Day to run
	public static boolean sleepDay(long day)
	{
		return sleepHours(day*24);
	}
	// After hh hours mi minutes ss seconds to run
	public static boolean sleepTime(long hours,long minutes,long seconds){
		return sleepHours(hours)&&sleepMinutes(minutes)&&sleepSeconds(seconds);
	}
	
	/**Please set a regular time.<br>
	 * 1)formatYears = 2015-10-20,formatTime = 10:20:15<br>
	 * 2)Procedures running  2015-10-20 10:20:15 <br>
	 * 设置时间，定时器，确定运行的时间
	 * @param formatYears
	 * @param formatTime
	 */
	public static boolean sleepTimer(String formatYears,String formatTime){
		long t3 = getSleepTimerVal(formatYears, formatTime);
		return sleep(t3);
	}
	/** 获取传入时间和当前时间的差值
	 * 
	 * @param formatYears
	 * @param formatTime
	 * @return
	 */
	public static long getSleepTimerVal(String formatYears,String formatTime){
		long t1 = readDateFromat(formatYears, formatTime);
		long t3 = getSleepTimerVal(t1);
		return t3;
	}
	/** 获取传入时间和当前时间的差值
	 * 
	 * @param time
	 * @return
	 */
	public static long getSleepTimerVal(long time){
		long t1 = time;
		long t2 = System.currentTimeMillis();
		long t3 = (t1-t2);
		return t3;
	}
	
	// TODO exe
	public static void main(String[] args) {
		long time = readDateFromat("2016-12-12", "23:45:00");
//		System.out.println(isLeapYear(2017));	// 是否是闰年
//		System.out.println(getMonthDaySize(2016, 2)); // 获得当前月份的天数
//		System.out.println(getMonthDaySize(2017, 2)); // 获得当前月份的天数
		System.out.println(getHour(time));			// 获得当前时间戳的小时
		System.out.println(getFormatDate(getDayStart(1)));
		System.out.println(getFormatDate(getDayStart(0)));
		System.out.println(getFormatDate(getWeekDayStart(1)));
		System.out.println(getFormatDate(getWeekDayStart(2)));
		
	}
	// Timer [ 定时]
	public static boolean sleepTimerByToday(String formatTime){
		Date date = new Date(System.currentTimeMillis());
		return sleepTimer(getFormatDate(date),formatTime);
	}
	public static boolean sleepTimerByNextTime(String formatTime){
		return sleepTimerByNextTime(formatTime, 1);
	}
	public static boolean sleepTimerByNextTime(String formatTime,int day){
		if(day == 0){
			day = 1;
		}
		Date date = new Date(System.currentTimeMillis());
		long t3 = getSleepTimerVal(getFormatDate(date), formatTime);
		if(t3<0){
			date.setDate(date.getDate()+day);
			t3 =  getSleepTimerVal(getFormatDate(date), formatTime);
		}
		System.out.println(getHour(t3));
		System.out.println(getMinute(t3));
		System.out.println(getSecond(t3));
		return sleep(t3);
	}
	
	/** 获取当前年月日
	 * 
	 * @param date
	 * @param fg 分隔符
	 * @return
	 */
	public static String getFormatDate(Date date,String fg){
		
		StringBuffer sb = new StringBuffer();
		int year = getYear(date);
		int month = getMonth(date);
		int day = getDay(date);
		sb.append(year);
		sb.append(fg);
		if(month<10)
			sb.append("0");
		sb.append(month);
		sb.append(fg);
		if(day<10)
			sb.append("0");
		sb.append(day);
		
		return sb.toString();
	}
	public static String getFormatDate(long time){
		return getFormatDate(new Date(time));
	}
	public static String getFormatDate(Date date){
		return getFormatDate(date,"-");
	}
	/**format = 19:10:20	[HH24:mm:ss]
	 * 
	 * @param format
	 * @return milli seconds
	 */
	public static int readTimeFormat(String format){
		if(format==null||format.length()<6)
			return 0;
		StringBuffer sb = new StringBuffer(6);
		for (int i = 0; i < format.length(); i++) {
			char c = format.charAt(i);
			if(c>='0'&&c<='9'){
				sb.append(c);
			}
		}
		
		String time = sb.toString();
		if(time.length()<6)
			throw new TimeFormatException(format);
		int hh = (time.charAt(0)-48)*10+time.charAt(1)-48;
		int mi = (time.charAt(2)-48)*10+time.charAt(3)-48;
		int ss = (time.charAt(4)-48)*10+time.charAt(5)-48;
		return (hh*60*60+mi*60+ss)*1000;
	}
	/**format = 2015-06-16	[yyyy-MM-dd]
	 * 
	 * @param format
	 * @return milli seconds
	 */
	public static long readYearsFormat(String format){
		if(format == null)
			return 0;
		if(format.length()<8)
			return 0;
		StringBuffer sb = new StringBuffer(6);
		for (int i = 0; i < format.length(); i++) {
			char c = format.charAt(i);
			if(c>='0'&&c<='9'){
				sb.append(c);
			}
		}
		
		String time = sb.toString();
		if(time.length()<8)
			throw new YearFormatException(format);
		
		int yy1 = (time.charAt(0)-48)*10+time.charAt(1)-48;
		int yy2 = (time.charAt(2)-48)*10+time.charAt(3)-48;
		int mm = (time.charAt(4)-48)*10+time.charAt(5)-48;
		int dd = (time.charAt(6)-48)*10+time.charAt(7)-48;
		int yy = yy1*100+yy2;
		@SuppressWarnings("deprecation")
		Date da = new Date(yy-1900, mm-1, dd);
		return da.getTime();
	}
	/**yearFormat = 2015-06-16	[yyyy-MM-dd]<br>
	 * timeFormat = 19:10:20	[HH24:mm:ss]
	 * 
	 * @param yearFormat	2015-06-16	[yyyy-MM-dd]
	 * @param timeFormat	19:10:20	[HH24:mm:ss]
	 * @return milli seconds 1444479620000 ms
	 */
	public static long readDateFromat(String yearFormat,String timeFormat){
		return readYearsFormat(yearFormat)+readTimeFormat(timeFormat);
	}
	
	/** 获取这一天的开始时间
	 * 
	 * @param nextday 从今天算起往后的几天，0代表今天，1代表明天以此类推
	 * @return
	 */
//	@SuppressWarnings("deprecation")
	public static long getDayStart(int nextday){
		return getDayStart(System.currentTimeMillis(),nextday);
	}
	public static long getDayStart(long timec,int nextday){
		long time = timec+nextday*oneDateTime;
		long time2 = time-(time+zoneTime)%oneDateTime;
		return time2;
	}
	/** 获取这周的开始
	 * 
	 * @param nextWeek 从本周开始算起 多少周，0代表本周 1 代表下一周 以此类推
	 * @return
	 */
	public static long getWeekDayStart(int nextWeek){
		int week_x = getWeek(nextWeek) - 1;
		return getDayStart(nextWeek*7) - week_x*oneDateTime;
	}
	
	public static int getWeek(int nextWeek){
		Date date = new Date(System.currentTimeMillis());
		date.setDate(date.getDate()+nextWeek*7);
		int week = date.getDay();
		if(week == 0){
			week = 7;
		}
		return week;
	}
	/** 结束时间
	 * 
	 * @param nextday 从今天算起往后几天结束时间，0代表今天结束时间，1代表明天结束时间
	 * @return
	 */
	public static long getDayEnd(int nextday){
		return getDayStart(nextday+1)-1; // 第二天的开始时间减 1 毫秒
	}
	
//	public static long getLastTimetoNowTime(String yearFormat,String timeFormat){return 0;}
	/** 获取一个传入时间小时
	 * 
	 * @param time
	 * @return
	 */
	public static int getHour_base(long time){
		return (int) (time/1000/60/60%24);
	} 
	
	/** 获取一个时间戳的小时，得到结果为 0-23
	 * 
	 * @param time
	 * @return
	 */
	public static int getHour(long time){
		
		return (int) ((time+zoneTime)/1000/60/60%24);
	} 
	public static int getMinute(long time){
		return (int) (time/1000/60%60);
	} 
	public static int getSecond(long time){
		return (int) (time/1000%60);
	} 
	public static int getMillisecond(long time){
		return (int) (time%1000);
	}
	
	/** 今年第几周
	 * 
	 * @param date
	 * @return
	 */
	public static int getYearWeek(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	/** 是否是闰年
	 * 
	 * @param year
	 * @return
	 */
	public static boolean isLeapYear(int year){
		if(year%100 == 0){
			return year%400 == 0;
		}else{
			return year%4 == 0;
		}
	}
	/** 获取这个月的天数
	 * 
	 * @param month
	 * @return
	 */
	public static int getMonthDaySize(int year,int month){
		if(month > 12 || month <1){
			return 0;
		}
		if(isLeapYear(year)){
			return leapYearDays[month-1];
		}else{
			return nonleapYearDays[month-1];
		}
	}
	
	public static class TimeFormatException extends RuntimeException{
		private static final long serialVersionUID = 1L;

	public TimeFormatException(String msg) {
		super("["+msg+"] The correct format is HH24-mm-ss");
	}}
	public static class YearFormatException extends RuntimeException{
		private static final long serialVersionUID = 1L;

	public YearFormatException(String msg) {
		super("["+msg+"] The correct format is yyyy-MM-dd");
	}}
	
}
